package com.china08.yxyapi.config;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

import redis.clients.jedis.JedisPoolConfig;

/**
 * Cache Config: RedisCache
 * 
 * @author Ertao.Fang
 *
 */
// @Profile("oth")
@Profile(value = { "dev", "tes", "pro", "prohrb" })
@Configuration
public class CacheConfigForRedisCache extends CacheConfig {

    public static final Logger LOGGER = LoggerFactory.getLogger(CacheConfigForRedisCache.class);

    @Bean
    CacheManager cacheManager2(RedisOperations<Object, Object> redisOperations) {
        List<String> cacheNames = new ArrayList<String>();
        cacheNames.add(DCN);
        RedisCacheManager manager = new RedisCacheManager(redisOperations);
        manager.setCacheNames(cacheNames);
        manager.setDefaultExpiration(60 * 2);// 过期时间2分钟
        // manager.setDefaultExpiration(60 * 10);// 过期时间10分钟
        // manager.setDefaultExpiration(60 * 60 * 1);// 过期时间1个小时
        // manager.setDefaultExpiration(60 * 60 * 12);// 过期时间12个小时
        return manager;
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.redis")
    public JedisPoolConfig getRedisConfig() {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setSoftMinEvictableIdleTimeMillis(1000 * 60 * 2);
        return config;
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.redis")
    public JedisConnectionFactory getConnectionFactory() {
        JedisConnectionFactory factory = new JedisConnectionFactory();
        JedisPoolConfig config = getRedisConfig();
        factory.setPoolConfig(config);
        return factory;
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.redis")
    public RedisTemplate<?, ?> getRedisTemplate() {
        RedisTemplate<?, ?> template = new StringRedisTemplate(getConnectionFactory());
        return template;
    }

}
